package com.zhl.service_.impl;

import com.zhl.dao_.RoleMapper;
import com.zhl.dao_.UserMapper;
import com.zhl.entity.Role;
import com.zhl.entity.User;
import com.zhl.service_.RoleService;
import com.zhl.service_.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/11/6.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> rolelist() {

        return roleMapper.rolelist();
    }

    @Override
    public Role queryRoleByid(String id) {
        return roleMapper.queryRoleByid(id);
    }


    //需要放在一个事物里
    @Override
    public int addRoleFunction(String rid, String fids) {

//        Role role = queryRoleByid(rid);
//        role.setFids(fids);
//        updateRole(role);

        if (StringUtils.isNotEmpty(fids)) {

            //mybatis没有类似hibernate的自动维护中间表,需要先删除,免得重复
            roleMapper.deleteRoleFunctionByRid(rid);

            String[] split = fids.split(",");
            for (String fid : split) {
                if (!fid.equals("all")) {
                    roleMapper.addRoleFunction(rid,fid);
                }
            }

        }

        return 1;
    }

    @Override
    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }
}
